package com.younion.action.consumer.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IRegioninfoService;
import com.younion.model.AreaResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.RegioninfoVO;

public class AreaGetConsumer extends AbstractRequestConsumer {
	private IRegioninfoService regioninfoService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return seekPsd(request, response);
	}

	public String seekPsd(HttpServletRequest request,
			HttpServletResponse response) {
		List<AreaResult> areaResultList = new ArrayList<AreaResult>();
		String province_id = request.getParameter("province_id");
		String city_id = request.getParameter("city_id");
		String district_id = request.getParameter("district_id");
		Map paramMap = new HashMap();
		if(StringUtils.isNotEmpty(district_id)){
			paramMap.put("regionId", district_id);
		}else if(StringUtils.isNotEmpty(city_id)){
			paramMap.put("pid", city_id);
		}else if(StringUtils.isNotEmpty(province_id)){
			paramMap.put("pid", province_id);
		}else{//不传参数则为查所有省
			paramMap.put("pid", 0);
		}
		ResultObject resultObject = new ResultObject();
		try {
			List<RegioninfoVO> regioninfoVOList = (List<RegioninfoVO>) regioninfoService
					.getByMap(paramMap);
			if (regioninfoVOList != null && regioninfoVOList.size() > 0) {
				for (RegioninfoVO regioninfoVO : regioninfoVOList) {
					AreaResult areaResult = new AreaResult();
					areaResult.setAreaId(regioninfoVO.getRegionId());
					areaResult.setAreaName(regioninfoVO.getRegion());
					areaResultList.add(areaResult);
				}
			}
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(areaResultList);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setRegioninfoService(IRegioninfoService regioninfoService) {
		this.regioninfoService = regioninfoService;
	}

}
